﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Entity;
using Service;
using Web.Framework;
using Entity.Admin;
using Core;

namespace Web.Areas.Admin.Controllers
{
    public partial class RoleController : BaseController
    {
        private RoleService roleService;

        public RoleController()
        {
            roleService = new RoleService(db);
        }
        //
        // GET: /Admin/Role/

        public virtual ViewResult Index(int? pageIndex, string keyword)
        {
            if (string.IsNullOrEmpty(keyword))
            {
                var roles = db.Roles.OrderBy(p => p.RoleName);
                return View(roles.ToPagedList(pageIndex));
            }
            else
            {
                var roles = db.Roles.Where(p => p.RoleName.Equals(keyword) || p.RoleMemo.Equals(keyword) || p.RoleName.StartsWith(keyword)).OrderBy(p => p.RoleName);
                return View(roles.ToPagedList(pageIndex));
            }
        }

        //
        // GET: /Admin/Role/Edit/5

        public virtual ActionResult Edit(string id)
        {
            Role role = db.Roles.Find(id);
            ViewBag.users = db.Users.ToList();
            return View(role);
        }

        //
        // POST: /Admin/Role/Edit/5

        [HttpPost]
        public virtual ActionResult Edit(Role role, FormCollection frm)
        {
            ViewBag.users = db.Users.ToList();
            var roleUserIds = frm.AllKeys.Where(p => p.StartsWith("UserId") && frm[p].Contains("true")).Select(p => p.Replace("UserId", ""));
            var roleMenus = frm["RoleMenus"].Split(',');
            if (ModelState.IsValid)
            {
                //db.Entry(role).State = EntityState.Modified;
                //db.SaveChanges();
                //return RedirectToAction("Index");
                try
                {
                    role = roleService.CreateOrSave(role);
                    roleService.SaveRoleUsers(role, roleUserIds);
                    roleService.SaveRoleMenus(role, roleMenus);
                    this.AddViewMessage("编辑成功。");
                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    this.AddViewMessage(ex);
                }
            }
            return View(role);
        }

        //
        // GET: /Admin/Role/Delete/5

        public virtual ActionResult Delete(string id)
        {
            Role role = db.Roles.Find(id);
            return View(role);
        }

        //
        // POST: /Admin/Role/Delete/5

        [HttpPost, ActionName("Delete")]
        public virtual ActionResult DeleteConfirmed(string id)
        {
            Role role = db.Roles.Find(id);
            db.Roles.Remove(role);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}